﻿// ******************************************************************
// * Copyright(c)  Steven Lu
// * Description   : Entity_PROD_USED_AMOUNT_TEMP
// * CreateDate    : 2013-02-16 15:20:26
// * Creater       : CodeSmith
// * LastChangeDate: 2013-02-16 15:20:26
// * LastChanger   : 2013-02-16 15:20:26
// * Version Info  : 1.0 For ESOL
// ******************************************************************

using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Suzsoft.Smart.Data;
using ESOL.Common.Entity;

namespace ESOL.Business.Prod
{
    /// <summary>
    /// ProdUsedAmountTemp
    /// </summary>
    public partial class ProdUsedAmountTempBs
    {
        #region GetByID
        /// <summary>
        /// Get Entity
        /// </summary>
        /// <param name="objProdUsedAmountTempEntity">Entity</param>
        /// <returns>Entity</returns>
        public static ProdUsedAmountTempEntity GetProdUsedAmountTemp(ProdUsedAmountTempEntity objProdUsedAmountTempEntity)
        {
            return DataAccess.SelectSingle<ProdUsedAmountTempEntity>(objProdUsedAmountTempEntity);
        }
        #endregion


        #region Insert
        /// <summary>
        /// Insert Data
        /// </summary>
        /// <param name="objProdUsedAmountTempEntity">Entity</param>
        /// <returns>true Insert Succeed; false Insert Fail</returns>
        public static bool InsertProdUsedAmountTemp(ProdUsedAmountTempEntity objProdUsedAmountTempEntity)
        {
            return DataAccess.Insert(objProdUsedAmountTempEntity);
        }

        public static bool InsertProdUsedAmountTemp(List<ProdUsedAmountTempEntity> objProdUsedAmountTempEntityList)
        {
            return DataAccess.Insert(objProdUsedAmountTempEntityList);
        }
        #endregion


        #region Update
        /// <summary>
        /// Update Data
        /// </summary>
        /// <param name="objProdUsedAmountTempEntity">Entity</param>
        /// <returns>true Update Succeed; false Update Fail</returns>
        public static bool UpdateProdUsedAmountTemp(ProdUsedAmountTempEntity objProdUsedAmountTempEntity)
        {
            return DataAccess.Update(objProdUsedAmountTempEntity);
        }

        public static bool UpdateProdUsedAmountTemp(List<ProdUsedAmountTempEntity> objProdUsedAmountTempEntityList)
        {
            return DataAccess.Update<ProdUsedAmountTempEntity>(objProdUsedAmountTempEntityList);
        }
        #endregion


        #region Delete
        /// <summary>
        /// Delete Data
        /// </summary>
        /// <param name="objProdUsedAmountTempEntity">Entity</param>
        /// <returns>true Delete Succeed; false Delete Fail</returns>
        public static bool DeleteProdUsedAmountTemp(ProdUsedAmountTempEntity objProdUsedAmountTempEntity)
        {
            return DataAccess.Delete(objProdUsedAmountTempEntity);
        }

        public static bool DeleteProdUsedAmountTemp(List<ProdUsedAmountTempEntity> objProdUsedAmountTempEntityList)
        {
            return DataAccess.Delete<ProdUsedAmountTempEntity>(objProdUsedAmountTempEntityList);
        }

        public static void DeleteProdUsedAmountTemp(List<ProdUsedAmountTempEntity> objProdUsedAmountTempEntityList,DataAccessBroker broker)
        {
            try
            {
                DataAccess.Delete<ProdUsedAmountTempEntity>(objProdUsedAmountTempEntityList, broker);
            }
            catch (Exception ex)
            {
                throw new Exception("删除生产占用量失败,"+ex.Message+"!");
            }
        }
        #endregion


        #region GetAll
        /// <summary>
        /// Get All Data
        /// </summary>
        /// <returns>Back All Data<returns>
        public static DataSet GetProdUsedAmountTempAll()
        {
            return DataAccess.SelectAllDataSet<ProdUsedAmountTempEntity>();
        }
        /// <summary>
        /// Get All Data List
        /// </summary>
        /// <returns>Back All Data List<returns>
        public static List<ProdUsedAmountTempEntity> GetProdUsedAmountTempAllList()
        {
            return DataAccess.SelectAll<ProdUsedAmountTempEntity>();
        }
        #endregion

        #region Functions
        /// <summary>
        /// 插入生产占用量数据
        /// </summary>
        /// <param name="dtBom">物料清单</param>
        /// <param name="dtWorkingOrder">生单的工令单集</param>
        /// <param name="dtPurchase">生单的采购申请集</param>
        /// <param name="broker">事务</param>
        /// <returns></returns>
        public static bool InsertProdUsedAmountTemp(DataTable dtBom, DataTable dtWorkingOrder, DataTable dtPurchase,DataAccessBroker broker)
        {
            List<ProdUsedAmountTempEntity> usedAmountTemps = new List<ProdUsedAmountTempEntity>();
            foreach (DataRow drBom in dtBom.Rows)
            {
                DataRow[] aryWorkingOrder = dtWorkingOrder.Select("BOM_ID='"+drBom["BOM_ID"].ToString()+"'");
                DataRow[] aryPurchase = dtPurchase.Select("BOM_ID='" + drBom["BOM_ID"].ToString() + "'");
                if (aryWorkingOrder.Length != 0)
                {
                    DataRow[] aryTemp = dtWorkingOrder.Select("BOM_ID='" + drBom["PARENT_BOM_ID"].ToString()+"'");
                    if (aryTemp.Length == 0)
                    {
                        aryTemp = dtPurchase.Select("BOM_ID='" + drBom["PARENT_BOM_ID"].ToString() + "'");
                        if(aryTemp.Length==0)
                            continue;
                    }
                    ProdUsedAmountTempEntity m = new ProdUsedAmountTempEntity();
                    m.ProductId = drBom["PRODUCT_ID"].ToString();
                    m.WorkingOrderId = aryWorkingOrder[0]["PLAN_ORDER_ITEM_ID"].ToString();
                    m.ParentWorkingOrderId = aryTemp[0]["PLAN_ORDER_ITEM_ID"].ToString();
                    m.Qty = Convert.ToDecimal(aryWorkingOrder[0]["PLAN_QTY"].ToString());
                    usedAmountTemps.Add(m);
                }
                else if (aryPurchase.Length != 0)
                {
                    DataRow[] aryTemp = dtPurchase.Select("BOM_ID='" + drBom["PARENT_BOM_ID"].ToString()+"'");
                    if (aryTemp.Length == 0)
                    {
                        aryTemp = dtWorkingOrder.Select("BOM_ID='" + drBom["PARENT_BOM_ID"].ToString() + "'");
                        if (aryTemp.Length == 0)
                        continue;
                    }
                    ProdUsedAmountTempEntity m = new ProdUsedAmountTempEntity();
                    m.ProductId = drBom["PRODUCT_ID"].ToString();
                    m.WorkingOrderId = aryPurchase[0]["APPLICATION_ITEM_ID"].ToString();
                    m.ParentWorkingOrderId = aryTemp[0]["PLAN_ORDER_ITEM_ID"].ToString();
                    m.Qty = Convert.ToDecimal(aryPurchase[0]["PLAN_QTY"].ToString());
                    usedAmountTemps.Add(m);
                }
            }
            try
            {
                DataAccess.Insert<ProdUsedAmountTempEntity>(usedAmountTemps, broker);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        /// <summary>
        /// 根据产品编码ID，查询其生产占用量
        /// </summary>
        /// <param name="sProductID"></param>
        /// <returns></returns>
        public static decimal GetProductionConsumptionforID(string sProductID)
        {
            string sql = @"select * from V_PRODUCTION_CONSUMPTION where product_id='"+sProductID+"'";
            DataSet ds=DataAccess.SelectDataSet(sql);
            if (ds == null)
                return 0;
            if (ds.Tables[0].Rows.Count == 0)
                return 0;
            return Convert.ToDecimal(ds.Tables[0].Rows[0]["QTY"].ToString());
        }

        /// <summary>
        /// 根据父工令单ID，查询实体集
        /// </summary>
        /// <param name="sWorkingOrderID">父工令单ID</param>
        /// <returns></returns>
        public static List<ProdUsedAmountTempEntity> GetAllForParentID(string sWorkingOrderID)
        {
            string sql = @"select * from PROD_USED_AMOUNT_TEMP where PARENT_WORKING_ORDER_ID='"+sWorkingOrderID+"'";
            return DataAccess.Select<ProdUsedAmountTempEntity>(sql);
        }

        #endregion
    }


}

